﻿using System;
using Microsoft.Xna.Framework;
using System.Runtime.Serialization;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;

namespace Rogue
{
    // Cute little bunnies around your house after you've won the game. They're fast and brainless.
    [Serializable]
    public class Bunny : Entity
    {
        public Bunny()
            : base()
        {
            AddBunnyBody();
        }
        public Bunny(string a_sName)
            : base(a_sName)
        {
            AddBunnyBody();
        }

        private void AddBunnyBody()
        {
            m_BodyRoot = new Torso(this);
            Head head = new Head(m_BodyRoot);

            new Leg(m_BodyRoot, "cute");

            m_BodyRoot.MultMaxHealth(0.2f);

            m_BodyRoot.OnSubPartLoss += SubPartLossHandler;
            m_BodyRoot.OnSubPartHealthChange += BodyHealthChange;

            m_BodyRoot.InitAttackIndices();
        }


        // this is what can kill us.
        private void SubPartLossHandler(BodyPart a_Part)
        {
            if (a_Part.GetType() == typeof(Head) || a_Part.GetType() == typeof(Torso))
            {
                m_IsAlive = false;
            }

            a_Part.MakeSeveredPart(this);
        }

        void BodyHealthChange(BodyPart a_Part, BodyPart.HealthChangeType a_Type, int a_DeltaHealth)
        {
            if (a_Type == BodyPart.HealthChangeType.Bleed)
            {
                RogueGame.Instance.Map.BloodyTerrain(X, Y, true);
            }
        }

        public override bool BlocksLight()
        {
            return false;
        }

        public override int GetMoveDelay()
        {
            foreach (Leg leg in m_BodyRoot.GetAllParts<Leg>())
            {
                if (leg.GetTotalHealth() > leg.GetTotalMaxHealth(true) / 2)
                    return m_nDelay;
            }

            return m_nDelay * 4;
        }

        static DetailWindowImages s_BunnyDetailWindowImages = new DetailWindowImages("Bunny");
        public override DetailWindowImages GetDetailWindowImages()
        {
            return s_BunnyDetailWindowImages;
        }

        public override void Think()
        {
            Direction d = Direction.GetDirection(Random.Instance.Next(-1, 2), Random.Instance.Next(-1, 2));
            if (d != null)
            {
                MoveDelta(d);
            }

            bool wasAlive = IsAlive;

            // this is kind of weird, we're pre-healing.
            // It should be m_nNextThink - m_nLastThink...
            int elapsedTime = m_nNextThink - Timeline.Instance.CurrentTime;
            m_BodyRoot.UpdateSubpartsHealth(elapsedTime);

            if (wasAlive && !IsAlive)
            {
                // TODO: have the battle reporter thing handle this
                ConsoleWindow.Instance.AddMessage(String.Format("{0} has bled to death.", Name), Color.Red);
                Die();
            }
        }

        #region Serialization
        public Bunny(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            // Overridden
        }
        #endregion
    }
}


